import random
import cv2
import numpy as np

lines = []
words = []
meanings = []
total = []
passed = []
stack = []
num = 0

choice = input("Choose: \n1.God Feet\n2.3000\n3.TOEFL 10000\n4.Notes\n5.base_1\n6.God Feet 2017\n7.God Feet 2018\n")
if choice == '1':
    start = input("Start Section No.?\n")
    end = input("End Section No.?\n")
    for i in range(int(start), int(end)+1):
        path = 'database/God_Feet/Section_' + str(i) + '.txt'
        files = open(path, 'r', encoding='utf8')
        lines += files.readlines()
elif choice == '3':
    start = input("Start List No.?\n")
    end = input("End List No.?\n")
    for i in range(int(start), int(end)+1):
        path = 'database/TOEFL_10000/List_' + str(i) + '.txt'
        files = open(path, 'r', encoding='utf8')
        lines += files.readlines()
elif choice == '4':
    start = input("Start List No.?\n")
    end = input("End List No.?\n")
    for i in range(int(start), int(end)+1):
        path = 'database/Notes/' + str(i) + '.txt'
        files = open(path, 'r', encoding='utf8')
        lines += files.readlines()
elif choice == '5':
    path = 'database/base_1/1.txt'
    files = open(path, 'r', encoding='utf8')
    lines += files.readlines()
elif choice == '6':
    start = input("Start Day No.?\n")
    end = input("End Day No.?\n")
    for i in range(int(start), int(end)+1):
        path = 'database/God_Feet_2017/Day_' + str(i) + '.txt'
        files = open(path, 'r', encoding='utf8')
        lines += files.readlines()
for line in lines:
    if num % 2 == 0:
        words += [line.split('\n')[0]]
        total += [len(words)-1]
    else:
        meanings += [line.split('\n')[0]]
    num += 1


key = 0
preIndex = 0
while key != 27:
    if len(passed) == len(words):
        break
    back = np.zeros([250, 600, 3], np.uint8)
    back[:,:,0] = np.ones([250, 600])*255
    back[:,:,1] = np.ones([250, 600])*255
    back[:,:,2] = np.ones([250, 600])*255
    unpassed = []
    for i in total:
        if i not in passed:
            unpassed += [i]
    # len(stack) is the limit of the total number of words that you do not remember
    if len(stack) == 20:
        index = stack[random.randint(0, len(stack)-1)]
        while preIndex == index:
            index = stack[random.randint(0, len(stack)-1)]
        preIndex = index
    else:
        index = unpassed[random.randint(0, len(unpassed)-1)]
        while preIndex == index and len(unpassed) > 1:
                index = unpassed[random.randint(0, len(unpassed)-1)]
        preIndex = index
        stack += [index]
    font = cv2.FONT_HERSHEY_SIMPLEX
    img = cv2.putText(back, words[index], (30, 150), font, 1, (0, 0, 0), 2)
    img = cv2.putText(back, "{}/{}/{}".format(len(stack), len(passed), len(total)), (400, 230), 
                                           font, 1.2, (0, 0, 0), 2)
    cv2.imshow("words", img)
    key = cv2.waitKey()
    while key != 122:
        cv2.imshow("words", img)
        if key == 32:
            print(meanings[index])
            key = cv2.waitKey()
        elif key == 13:
            if index not in passed:
                passed += [index]
            tempStack = []
            for i in stack:
                if i != index:
                    tempStack += [i]
            stack = tempStack
            key = 122
        elif key == 27:
            break
        else:
            key = cv2.waitKey()